home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / jpegagasrc / jpegaga / jpegaga.c < prev    next >
C/C++ Source or Header  |  1995-11-08  |  21KB  |  792 lines

  1. /* jpegAGA 2.2 written by Günther Röhrich */
  2.  
  3. #define __NOLIBBASE__
  4. #include <workbench/startup.h>
  5. #include <proto/dos.h>
  6. #include "WBFlags.h"
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. /*
  12.  * Include file for users of JPEG library.
  13.  * You will need to have included system headers that define at least
  14.  * the typedefs FILE and size_t before you can include jpeglib.h.
  15.  * (stdio.h is sufficient on ANSI-conforming systems.)
  16.  * You may also wish to include "jerror.h".
  17.  */
  18.  
  19. #include "jpeglib.h"
  20. #include "jerror.h"
  21. #include "myerror.h"
  22. #define JMAKE_MSG_TABLE
  23. #include "myerror.h" /* create the message string table */
  24.  
  25. /*
  26.  * <setjmp.h> is used for the optional error recovery mechanism shown in
  27.  * the second part of the example.
  28.  */
  29.  
  30. #include <setjmp.h>
  31. #include <signal.h>
  32. #include <string.h>
  33.  
  34.  
  35. extern void Amiga_jpeg_stdio_src (j_decompress_ptr cinfo, BPTR infile);
  36.  
  37. #define HAM8 1
  38.  
  39. /* #define DEBUGFILE */ /* write a ppm file for debugging */
  40.  
  41. #ifdef __GNUC__
  42. #define MYSTRCMP strcasecmp
  43. #define MYSTRNCMP strncasecmp
  44. #else
  45. #define MYSTRCMP strcmp
  46. #define MYSTRNCMP strncmp
  47. #endif
  48.  
  49. #define MAXFILES 500
  50.  
  51. char  *ver = "\0$VER: jpegAGA 2.2 (15.9.95)";
  52.  
  53. #ifdef __GNUC__
  54. char __stdiowin[]="CON:30/12/600/80/jpegAGA 2.2";
  55. #endif
  56.  
  57.  
  58. struct WBFlags WBFlags =
  59. {
  60.  1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  61. };
  62.  
  63. struct WBFlags OldFlags;
  64.  
  65. short SMR_HAM;
  66. short WBFiles=0;
  67. short DoNotWait=0;
  68. short finish=0;
  69. static short ButtonPressed=0;
  70.  
  71. BPTR olddir=0;
  72. int NumPictures=0;
  73. struct WBArg *wb_arg=NULL;
  74. char *PicArray[MAXFILES];
  75. unsigned long SMR_DisplayID = 0;
  76. unsigned long *ScreenColorTable=NULL;
  77. short ASLPicSelect = 0;
  78.  
  79. static unsigned long Mode=0;
  80. static char *MapFileName=NULL;
  81. static char *MapDirName=NULL;
  82. static BPTR infile=0;
  83. static BPTR ColorMapFile = 0;
  84.  
  85. /* This struct contains the JPEG decompression parameters and pointers to
  86.  * working space (which is allocated as needed by the JPEG library).
  87.  */
  88.  
  89. static struct jpeg_decompress_struct cinfo;
  90.  
  91. /* We use our private extension JPEG error handler. */
  92.  
  93. static struct jpeg_error_mgr jerr;
  94.  
  95. volatile int PictureNr;
  96.  
  97. extern void FillNameBuffer(char *pattern);
  98. extern void ParseToolTypes(struct WBArg *wbarg, int initial);
  99. extern int InitDisplay(int cols, int rows, unsigned long Mode, int NumPlanes);
  100. extern void CloseDisplay(void);
  101. extern void DisplayRow(JSAMPROW buffer, int cols);
  102. extern int CheckButton(void);
  103. extern void FinalWait(void);
  104. extern void FlipScreen(void);
  105. extern void FreeASL(void);
  106. extern int ChooseScreenMode(int grayscale, char *title);
  107. extern void ChooseFiles(void);
  108. extern int FindScaleFit(JDIMENSION width, JDIMENSION height, unsigned long Mode);
  109. extern unsigned long my_screen[2];
  110. extern unsigned long ScaleFitDisplayID;
  111. JSAMPROW OutputBuffer=NULL;
  112.  
  113. extern void EncodeHAM8(JSAMPROW orig, JSAMPROW yham, int xsize);
  114. unsigned short Mult_Table[2*256];
  115. jmp_buf setjmp_buffer;
  116.  
  117. /* NOTE: this array is in brgbrg order */
  118.  
  119. char *ColorCache=NULL;
  120. unsigned char FixedColorTable[64*3] =
  121.  { 0, 0, 0,  4, 4, 4,  8, 8, 8, 12,12,12,   
  122.   16,16,16, 20,20,20, 24,24,24, 28,28,28,  /* 16 colors */
  123.   32,32,32, 36,36,36, 41,41,41, 46,46,46,
  124.   51,51,51, 55,55,55, 59,59,59, 63,63,63,
  125.  
  126.  
  127.                       17,17,39, 17,17,55, /* 13 colors */ 
  128.   17,29,17,           17,29,39, 17,29,55, 
  129.   17,39,17, 17,39,29, 17,39,39, 17,39,55,
  130.   17,55,17, 17,55,39, 17,55,39, 17,55,55,
  131.  
  132.  
  133.             29,17,29, 29,17,39, 29,17,55, /* 11 colors */
  134.                                 29,29,55,
  135.   29,39,17, 29,39,29,           29,39,55,
  136.   29,55,17, 29,55,29, 29,55,39, 29,55,55,
  137.  
  138.  
  139.   39,17,17, 39,17,29, 39,17,39, 39,17,55, /* 12 colors */
  140.   39,29,17, 39,29,29,           39,29,55,
  141.   39,39,17, 39,39,29,          
  142.   39,55,17, 39,55,29,  
  143.  
  144.   
  145.   55,17,17, 55,17,29, 55,17,39, 55,17,55, /* 13 colors */
  146.   55,29,27, 55,29,29, 55,29,39, 55,29,55,
  147.   55,39,17, 55,39,29, 55,39,39, 
  148.   55,55,17, 55,55,29
  149. };
  150.  
  151. unsigned char ColorTable[64*3];
  152.  
  153.  
  154.  
  155. METHODDEF void
  156. my_error_exit (j_common_ptr cinfo)
  157. {
  158.  #ifdef __GNUC__
  159.  signal(SIGINT, SIG_IGN);
  160.  #endif
  161.  /* Always display the message. */
  162.  if(cinfo->err->msg_code != JWRN_JPEGAGA_STOPPED) (*cinfo->err->output_message) (cinfo);
  163.  
  164.  /* Return control to the setjmp point */
  165.  longjmp(setjmp_buffer, 1);
  166. }
  167.  
  168. void interrupt(int signum)
  169. {
  170.  #ifdef __GNUC__
  171.  signal(SIGINT, SIG_IGN);
  172.  #endif
  173.  printf("^C\n");
  174.  ButtonPressed = 1;
  175.  longjmp(setjmp_buffer, 1);
  176. }
  177.  
  178.  
  179. GLOBAL int
  180. read_JPEG_file (char *filenames[])
  181. {
  182.   /* More stuff */
  183.   JSAMPARRAY buffer;        /* Output row buffer */
  184.   int row_stride;        /* physical row width in output buffer */
  185.   int DisplaySuccess, i;
  186.   char *filename;
  187.  
  188.   /* Step 1: allocate and initialize JPEG decompression object */
  189.   
  190.   jpeg_create_decompress(&cinfo);
  191.  
  192.   /* Now we can initialize the JPEG decompression object. */
  193.  
  194.   /* We set up the normal JPEG error routines, then override error_exit. */
  195.   cinfo.err = jpeg_std_error(&jerr);
  196.   jerr.error_exit = my_error_exit;
  197.  
  198.   /* Set maximum memory to use */
  199.   if(WBFlags.MaxMemFlag) cinfo.mem->max_memory_to_use = WBFlags.MaxMem * 1000L;
  200.   
  201.  
  202.   /* Add some application-specific error messages (from myerror.h) */
  203.   jerr.addon_message_table = addon_message_table;
  204.   jerr.first_addon_message = JMSG_FIRSTADDONCODE;
  205.   jerr.last_addon_message = JMSG_LASTADDONCODE;
  206.  
  207.   /* main loop */
  208.  
  209.   for(PictureNr=0; PictureNr < NumPictures; PictureNr++)
  210.   {
  211.  
  212.     /* Establish the setjmp return context for my_error_exit to use. */
  213.     if (setjmp(setjmp_buffer)) {
  214.       /* If we get here, the JPEG code has signaled an error.
  215.        * We need to clean up the JPEG object, close the input file, and return.
  216.        */
  217.       if(PictureNr == NumPictures -1)
  218.         jpeg_destroy_decompress(&cinfo);
  219.       else
  220.         jpeg_abort_decompress(&cinfo);
  221.       if(infile)
  222.       {
  223.         Close(infile);
  224.         infile=0;
  225.       }
  226.       if(olddir)
  227.       {
  228.         CurrentDir(olddir);
  229.         olddir=0;
  230.       }
  231.       if(ColorCache)
  232.       {
  233.         free(ColorCache);
  234.         ColorCache=NULL;
  235.       }
  236.       if(MapFileName)
  237.       {
  238.         free(MapFileName);
  239.         MapFileName=NULL;
  240.       }
  241.       if(MapDirName)
  242.       {
  243.         free(MapDirName);
  244.         MapDirName=NULL;
  245.       }
  246.       if(ColorMapFile)
  247.       {
  248.         Close(ColorMapFile);
  249.         ColorMapFile=0;
  250.       }
  251.       if(OutputBuffer)
  252.       {
  253.        free(OutputBuffer);
  254.        OutputBuffer=NULL;
  255.       }
  256.       if(ScreenColorTable)
  257.       {
  258.         free(ScreenColorTable);
  259.         ScreenColorTable=NULL;
  260.       }
  261.       if(PictureNr == NumPictures -1 || WBFlags.SMRenable)
  262.       {
  263.         if(!ButtonPressed) FinalWait();
  264.         CloseDisplay();
  265.         if(finish) break;
  266.       }
  267.       if(finish)
  268.       {
  269.         CloseDisplay();
  270.         break;
  271.       }
  272.       else if(ButtonPressed)
  273.         DoNotWait=1;
  274.       continue;
  275.     }
  276.  
  277.     /* CTRL-C handling) */
  278.  
  279.     #ifdef __GNUC__
  280.     signal(SIGINT, interrupt);
  281.     #endif
  282.     
  283.     if(olddir)
  284.     {
  285.       CurrentDir(olddir);
  286.       olddir=0;
  287.     }
  288.  
  289.     ScaleFitDisplayID = 0;
  290.  
  291.     if(WBFiles) memcpy(&WBFlags, &OldFlags, sizeof(struct WBFlags));
  292.  
  293.     if(WBFiles && wb_arg)
  294.     {
  295.       if(wb_arg->wa_Name == NULL) ERREXIT(&cinfo, JERR_JPEGAGA_OPEN);
  296.       filename = wb_arg->wa_Name;
  297.       printf(" %s: ", filename);
  298.       fflush(stdout);
  299.  
  300.       if(wb_arg->wa_Lock != 0)
  301.       {
  302.         olddir = CurrentDir(wb_arg->wa_Lock);
  303.         infile = Open(filename, MODE_OLDFILE);
  304.         if(infile) ParseToolTypes(wb_arg, 0);
  305.       }
  306.       else
  307.       {
  308.         infile = Open(filename, MODE_OLDFILE);    
  309.       }
  310.       wb_arg++;
  311.     }
  312.     else if(!WBFiles)
  313.     {
  314.       filename = filenames[PictureNr];
  315.       printf(" %s: ", filename);
  316.       fflush(stdout);
  317.       infile = Open(filename, MODE_OLDFILE);
  318.     }
  319.  
  320.     if(infile == 0)
  321.     {
  322.       ERREXIT(&cinfo, JERR_JPEGAGA_OPEN);
  323.     }
  324.     
  325.     /* Step 2: specify data source (eg, a file) */
  326.  
  327.     Amiga_jpeg_stdio_src(&cinfo, infile);
  328.  
  329.     /* Step 3: read file parameters with jpeg_read_header() */
  330.  
  331.     (void) jpeg_read_header(&cinfo, TRUE);
  332.     /* We can ignore the return value from jpeg_read_header since
  333.      *   (a) suspension is not possible with the stdio data source, and
  334.      *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
  335.      * See libjpeg.doc for more info.
  336.      */
  337.  
  338.     printf("%d x %d, ", (int)cinfo.image_width, (int)cinfo.image_height);
  339.     if(cinfo.progressive_mode) printf("progr., ");
  340.     fflush(stdout);
  341.  
  342.     /* Step 4: set parameters for decompression */
  343.  
  344.     /* set user selectable options */
  345.   
  346.     if(WBFlags.DCTFast) cinfo.dct_method = JDCT_IFAST; 
  347.  
  348.     if(WBFlags.NoSmooth) cinfo.do_fancy_upsampling = FALSE; /* override this with options */
  349.  
  350.     if(WBFlags.SMRenable)
  351.     {
  352.       int grayscale=0;
  353.       
  354.       if(cinfo.out_color_space == JCS_GRAYSCALE) grayscale = 1;
  355.       FinalWait();
  356.       CloseDisplay();
  357.       if(finish) ERREXIT(&cinfo, JWRN_JPEGAGA_STOPPED);
  358.       if(ChooseScreenMode(grayscale, filename)) ERREXIT(&cinfo, JERR_JPEGAGA_NOMODE);
  359.       if(SMR_HAM)
  360.         WBFlags.GrayEnable = 0;
  361.       else
  362.         WBFlags.GrayEnable = 1;
  363.     }
  364.     else if(WBFlags.ScaleFit)
  365.     {
  366.       int tempscale;
  367.       if(WBFlags.GrayEnable) cinfo.out_color_space=JCS_GRAYSCALE; /* force grayscale output */
  368.       if(cinfo.out_color_space != JCS_GRAYSCALE)
  369.         Mode = HAM8;
  370.       else
  371.         Mode = 0;
  372.       tempscale = FindScaleFit(cinfo.image_width, cinfo.image_height, Mode);
  373.       if(tempscale == -1) ERREXIT(&cinfo, JERR_JPEGAGA_DISPLAY);
  374.       WBFlags.scale = tempscale;
  375.     }
  376.   
  377.     if(WBFlags.GrayEnable) cinfo.out_color_space=JCS_GRAYSCALE; /* force grayscale output */
  378.     cinfo.scale_num = 1;
  379.     cinfo.scale_denom = (unsigned int)WBFlags.scale;
  380.  
  381.     if(WBFlags.scale != 1) printf("scale 1/%d, ", WBFlags.scale);
  382.  
  383.     /* Step 5: Start decompressor */
  384.  
  385.     jpeg_start_decompress(&cinfo);
  386.  
  387.     /* Now setup everything for showing the picture */
  388.  
  389.     if(cinfo.out_color_space == JCS_GRAYSCALE)
  390.     {
  391.       printf("grayscale\n");
  392.       fflush(stdout);
  393.       ScreenColorTable = malloc(256*4*3+8);
  394.       if(!ScreenColorTable) ERREXIT(&cinfo, JERR_JPEGAGA_MEMORY);
  395.  
  396.       for(i=0; i<256; i++)
  397.       {
  398.         ScreenColorTable[i*3+1] = (unsigned long)i<<24;
  399.         ScreenColorTable[i*3+2] = (unsigned long)i<<24;
  400.         ScreenColorTable[i*3+3] = (unsigned long)i<<24;
  401.       }
  402.  
  403.       ScreenColorTable[0] = 256L<<16+0;
  404.       ScreenColorTable[256*3+1] = 0;
  405.  
  406.       DisplaySuccess = InitDisplay(cinfo.output_width, cinfo.output_height, 0, 8);
  407.  
  408.       if(finish) ERREXIT(&cinfo, JWRN_JPEGAGA_STOPPED);
  409.  
  410.       if(ScreenColorTable)
  411.       {
  412.         free(ScreenColorTable);
  413.         ScreenColorTable=NULL;
  414.       }
  415.  
  416.       if(DisplaySuccess != 1)
  417.       {
  418.         CloseDisplay();
  419.         ERREXIT(&cinfo, JERR_JPEGAGA_DISPLAY);
  420.       }
  421.  
  422.     }
  423.     else if(cinfo.out_color_space == JCS_RGB)
  424.     {
  425.       printf("HAM8");
  426.       fflush(stdout);
  427.       ColorCache = calloc(262145, 1);
  428.       if(ColorCache == NULL) ERREXIT(&cinfo, JERR_JPEGAGA_MEMORY);
  429.  
  430.       /* create the multiplication table */
  431.       for(i=-255; i<256; i++) Mult_Table[i+255] = (unsigned short)(i*i);
  432.  
  433.       MapFileName = malloc(strlen(filename)+5);
  434.       if(MapFileName == NULL) ERREXIT(&cinfo, JERR_JPEGAGA_MEMORY);
  435.       
  436.       strcpy(MapFileName, filename); /* create a copy of the file name */
  437.  
  438.       strcat(MapFileName, ".map");
  439.  
  440.       ColorMapFile = Open(MapFileName, MODE_OLDFILE);
  441.  
  442.       /* try to find the file in the directory pointed to by the environment */
  443.       /* variable MAPDIR to support read-only devices like CD-ROM */
  444.       if(!ColorMapFile)
  445.       {
  446.         char *MapDir;
  447.         MapDir = getenv("MAPDIR");
  448.         if(MapDir)
  449.         if(strlen(MapDir) != 0)
  450.         {
  451.           int pos,i;
  452.           MapDirName = malloc(strlen(MapDir)+strlen(MapFileName)+5); /* worst case */
  453.           if(!MapDirName) ERREXIT(&cinfo, JERR_JPEGAGA_MEMORY);
  454.           strcpy(MapDirName, MapDir);
  455.  
  456.           i = strlen(MapDirName);
  457.           if(MapDirName[i-1] != '/' && MapDirName[i-1] != ':')
  458.           {
  459.             strcat(MapDirName, "/");
  460.             i++;
  461.           }
  462.           i = strlen(MapFileName);
  463.           while(i > 0 && MapFileName[i-1] != '/' && MapFileName[i-1] != ':') i--;
  464.           strcat(MapDirName, &MapFileName[i]);
  465.           /* printf("%s\n", MapDirName); */
  466.           ColorMapFile = Open(MapDirName, MODE_OLDFILE);
  467.         }
  468.       }
  469.  
  470.  
  471.       if(!ColorMapFile)
  472.       {
  473.         int i = strlen(MapFileName) - 4;
  474.         while(i > 0 && MapFileName[i-1] != '.') i--;
  475.         if(MapFileName[i-1] == '.')
  476.         {
  477.           strcpy(&MapFileName[i], "map");
  478.           ColorMapFile = Open(MapFileName, MODE_OLDFILE);
  479.         }
  480.       }
  481.  
  482.  
  483.       if(ColorMapFile)
  484.       {
  485.         unsigned short MagicNumber;
  486.         unsigned int Reserved;
  487.  
  488.         if(Read(ColorMapFile, &MagicNumber, 2) != 2) ERREXIT(&cinfo, JERR_JPEGAGA_MAPFILE);
  489.         if(MagicNumber != 0x1203) ERREXIT(&cinfo, JERR_JPEGAGA_MAPFILE);  
  490.         if(Read(ColorMapFile, &Reserved, 4) != 4) ERREXIT(&cinfo, JERR_JPEGAGA_MAPFILE);
  491.         if(Read(ColorMapFile, ColorTable,  64*3) != 64*3) ERREXIT(&cinfo, JERR_JPEGAGA_MAPFILE);
  492.  
  493.         printf(" with mapfile\n");
  494.         Close(ColorMapFile);
  495.  
  496.         ColorMapFile = 0;
  497.       }
  498.       else
  499.       {
  500.         printf(", create a colormap file for better quality!\n");
  501.         memcpy(ColorTable, FixedColorTable, 64*3);
  502.       }
  503.  
  504.       if(MapFileName)
  505.       {
  506.         free(MapFileName);
  507.         MapFileName=NULL;
  508.       }
  509.       if(MapDirName)
  510.       {
  511.         free(MapDirName);
  512.         MapDirName=NULL;
  513.       }
  514.  
  515.       ScreenColorTable = malloc(64*4*3+8);
  516.       if(!ScreenColorTable) ERREXIT(&cinfo, JERR_JPEGAGA_MEMORY);
  517.  
  518.       for(i=0; i<64; i++)
  519.       {
  520.         ScreenColorTable[i*3+1] = (unsigned long)ColorTable[i*3+1]<<26; 
  521.         ScreenColorTable[i*3+2] = (unsigned long)ColorTable[i*3+2]<<26;
  522.         ScreenColorTable[i*3+3] = (unsigned long)ColorTable[i*3]<<26;
  523.       }
  524.  
  525.       ScreenColorTable[0] = 64L<<16+0;
  526.       ScreenColorTable[64*3+1] = 0;
  527.  
  528.       DisplaySuccess = InitDisplay(cinfo.output_width, cinfo.output_height, HAM8, 8);
  529.       
  530.       if(finish) ERREXIT(&cinfo, JWRN_JPEGAGA_STOPPED);
  531.        
  532.       if(ScreenColorTable)
  533.       {
  534.         free(ScreenColorTable);
  535.         ScreenColorTable=NULL;
  536.       }
  537.  
  538.       if(DisplaySuccess != 1)
  539.       {
  540.         CloseDisplay();
  541.         ERREXIT(&cinfo, JERR_JPEGAGA_DISPLAY);
  542.       }
  543.     }
  544.     else ERREXIT(&cinfo, JERR_CONVERSION_NOTIMPL); 
  545.  
  546.     /* JSAMPLEs per row in output buffer */
  547.     /* make it a bit larger to allow direct screen output */
  548.  
  549.     row_stride = (((cinfo.output_width * cinfo.output_components +15)>>4)<<4);
  550.  
  551.     /* Make a sample array that will go away when done with image */
  552.     buffer = (*cinfo.mem->alloc_sarray)
  553.           ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, cinfo.rec_outbuf_height);
  554.  
  555.     /* printf("Recommended size: %d\n", cinfo.rec_outbuf_height); */
  556.  
  557.     /* Step 6: while (scan lines remain to be read) */
  558.     /*           jpeg_read_scanlines(...); */
  559.  
  560.     /* Here we use the library's state variable cinfo.output_scanline as the
  561.      * loop counter, so that we don't have to keep track ourselves.
  562.      */
  563.  
  564.     ButtonPressed=0;
  565.  
  566.     if(cinfo.out_color_space == JCS_GRAYSCALE)
  567.     {
  568.       JDIMENSION numpixels, i;
  569.       while (cinfo.output_scanline < cinfo.output_height)
  570.       {
  571.         numpixels = jpeg_read_scanlines(&cinfo, buffer, cinfo.rec_outbuf_height);
  572.         if(CheckButton())
  573.         {
  574.           ButtonPressed=1;
  575.           ERREXIT(&cinfo, JWRN_JPEGAGA_STOPPED);
  576.         }
  577.         /* Assume put_scanline_someplace wants a pointer and sample count. */
  578.         for(i=0; i<numpixels; i++)
  579.           DisplayRow(buffer[i], cinfo.output_width);
  580.       }
  581.     }
  582.  
  583.     else if(cinfo.out_color_space == JCS_RGB)
  584.     {
  585.       JDIMENSION numpixels, i;
  586. #ifdef DEBUGFILE
  587.       FILE *testfile;
  588.       testfile = fopen("test.ppm", "w");
  589.       if(testfile == NULL) ERREXIT(&cinfo, JERR_JPEGAGA_MEMORY);
  590.       fprintf(testfile, "P6\n%ld %ld\n%d\n",
  591.           (long) cinfo.output_width, (long) cinfo.output_height, 255);
  592. #endif
  593.  
  594.       OutputBuffer = malloc(((cinfo.output_width+15)>>4)<<4);
  595.       if(!OutputBuffer) ERREXIT(&cinfo, JERR_JPEGAGA_MEMORY);
  596.     
  597.     
  598.       while (cinfo.output_scanline < cinfo.output_height)
  599.       {
  600.         numpixels = jpeg_read_scanlines(&cinfo, buffer, cinfo.rec_outbuf_height);
  601.         if(CheckButton())
  602.         {
  603.           ButtonPressed=1;
  604.           ERREXIT(&cinfo, JWRN_JPEGAGA_STOPPED);
  605.         }
  606.         /* Assume put_scanline_someplace wants a pointer and sample count. */
  607.         for(i=0; i<numpixels; i++)
  608.         {
  609. #ifdef DEBUGFILE
  610.           fwrite(buffer[i], 3, cinfo.output_width, testfile);
  611. #endif
  612.           EncodeHAM8(buffer[i], OutputBuffer, cinfo.output_width);
  613.           DisplayRow(OutputBuffer, cinfo.output_width);
  614.         }
  615.       }
  616. #ifdef DEBUGFILE
  617.       fclose(testfile);
  618. #endif
  619.       free(OutputBuffer);
  620.       OutputBuffer = NULL;
  621.     }
  622.  
  623.  
  624.     /* Step 7: Finish decompression */
  625.   
  626.     (void) jpeg_finish_decompress(&cinfo);
  627.     /* We can ignore the return value since suspension is not possible
  628.      * with the stdio data source.
  629.      */
  630.  
  631.     /* Step 8: Release JPEG decompression object */
  632.  
  633.     #ifdef __GNUC__
  634.     signal(SIGINT, SIG_IGN);
  635.     #endif
  636.  
  637.  
  638.     /* This is an important step since it will release a good deal of memory. */
  639.     if(PictureNr == NumPictures -1) jpeg_destroy_decompress(&cinfo);
  640.  
  641.     /* After finish_decompress, we can close the input file.
  642.      * Here we postpone it until after no more JPEG errors are possible,
  643.      * so as to simplify the setjmp error logic above.  (Actually, I don't
  644.      * think that jpeg_destroy can do an error exit, but why assume anything...)
  645.      */
  646.     if(olddir)
  647.     {
  648.       CurrentDir(olddir);
  649.       olddir=0;
  650.     }
  651.     if(infile)
  652.     {
  653.       Close(infile);
  654.       infile=0;
  655.     }
  656.     if(ColorCache)
  657.     {
  658.       free(ColorCache);
  659.       ColorCache=NULL;
  660.     }
  661.  
  662.     DoNotWait=0;
  663.     if(PictureNr == NumPictures -1)
  664.     {
  665.       FinalWait();
  666.       CloseDisplay();
  667.     }
  668.     if(finish) break;
  669.  
  670.     /* At this point you may want to check to see whether any corrupt-data
  671.      * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
  672.      */
  673.  
  674.     /* And we're done! */
  675.   }
  676.   CloseDisplay(); /* just for the case we forgot to close something */
  677.   return 0;
  678. }
  679.  
  680.  
  681. static void Usage(void)
  682. {
  683.  printf("Usage: jpegAGA [switches] [files]\n"
  684.         "switches: -gray      force grayscale output\n"
  685.         "          -fit       scale output to fit on the screen\n"
  686.         "          -scale M/N scale output image by fraction M/N, eg. 1/8\n"
  687.         "          -nosmooth  don't use high quality upsampling\n"
  688.         "          -dct fast  use fast integer DCT (less accurate)\n"
  689.         "          -vga       use VGA screenmode\n"
  690.         "          -super72   use SUPER72 mode for large pictures\n"
  691.         "          -smr       use screen mode requester\n"
  692.         "          -maxmem N  maximum memory to use (for decoder)\n");
  693.  CloseDisplay();
  694.  exit(10);
  695. }
  696.  
  697. int main(int argc, char *argv[])
  698. {
  699.  int i;
  700.  
  701. #ifdef __GNUC__
  702.  signal(SIGINT, SIG_IGN); /* disable CTRL-C handling */
  703. #endif
  704.  
  705.  printf("jpegAGA 2.2 by Günther Röhrich. This program is Public Domain.\n");
  706.  
  707.  /* remove the comments for beta versions */
  708.  /* printf("Preliminary version. DO NOT SPREAD IT!\n"); */
  709.  
  710.  if(argc == 0 && argv) FillWB(argv);
  711.  
  712.  else for(i=1; i<argc; i++)
  713.  {
  714.    if(MYSTRNCMP(argv[i], "-", 1))
  715.    {
  716.      FillNameBuffer(argv[i]);
  717.      continue;
  718.    }
  719.    #ifndef __GNUC__
  720.    strupr(argv[i]);
  721.    #endif
  722.    if(!MYSTRNCMP(argv[i], "-GRAY", 5)) WBFlags.GrayEnable=1;   
  723.  
  724.    else if(!MYSTRNCMP(argv[i], "-VGA", 4)) WBFlags.VGAenable=1;
  725.  
  726.    else if(!MYSTRNCMP(argv[i], "-SUPER72", 8)) WBFlags.SUPER72enable=1;
  727.  
  728.    else if(!MYSTRNCMP(argv[i], "-SMR", 4)) WBFlags.SMRenable=1;
  729.  
  730.    else if(!MYSTRNCMP(argv[i], "-NOSMOOTH", 9)) WBFlags.NoSmooth=1;
  731.  
  732.    else if(!MYSTRNCMP(argv[i], "-FIT", 4)) WBFlags.ScaleFit=1;
  733.  
  734.    else if(!MYSTRNCMP(argv[i], "-DCT", 4))
  735.    {
  736.      if(argc == i+1) Usage(); /* last argument */
  737.      i++;
  738.       if(!MYSTRNCMP(argv[i], "FAST", 3)) WBFlags.DCTFast=1;
  739.       else if(!MYSTRNCMP(argv[i], "INT", 3)) WBFlags.DCTFast=0;
  740.    }
  741.  
  742.    else if(!MYSTRNCMP(argv[i], "-SCALE", 6))
  743.    {
  744.      if(argc == i+1) Usage(); /* last argument */
  745.      i++;
  746.       if(!MYSTRNCMP(argv[i], "1/1", 3)) WBFlags.scale=1;
  747.       else if(!MYSTRNCMP(argv[i], "1/2", 3)) WBFlags.scale=2;
  748.       else if(!MYSTRNCMP(argv[i], "1/4", 3)) WBFlags.scale=4;
  749.       else if(!MYSTRNCMP(argv[i], "1/8", 3)) WBFlags.scale=8;
  750.       else Usage();
  751.    }
  752.     
  753.    else if(!MYSTRNCMP(argv[i], "-MAXMEM", 7))
  754.    {
  755.      char ch = 'x';
  756.      if(argc == i+1) Usage(); /* last argument */
  757.      i++;
  758.      if (sscanf(argv[i], "%ld%c", &WBFlags.MaxMem, &ch) < 1) Usage();
  759.      if (ch == 'm' || ch == 'M') WBFlags.MaxMem *= 1000L;
  760.      WBFlags.MaxMemFlag = 1;
  761.    }
  762.  
  763.    else Usage();
  764.  }    
  765.  
  766.  if(NumPictures == 0) ChooseFiles();
  767.  
  768.  if(NumPictures == 0)
  769.  {
  770.    CloseDisplay();
  771.    FreeASL();
  772.    exit(10);
  773.  }
  774.  
  775.  while(NumPictures)
  776.  {
  777.    read_JPEG_file(PicArray);
  778.    finish=0;
  779.    for(i=0; i<NumPictures; i++)
  780.    {
  781.      free(PicArray[i]);
  782.      PicArray[i]=NULL;
  783.    }
  784.    NumPictures=0;
  785.    if(ASLPicSelect) ChooseFiles();
  786.  }
  787.  
  788.  CloseDisplay();
  789.  FreeASL();
  790.  return 0;
  791. }
  792.